import { observable, action, computed, runInAction, toJS } from 'mobx';

import BaseStore from './BaseStore';

class LayoutStore extends BaseStore {
    @observable isSideBarExpanded = false;
    @observable isSideBarToggleShow = false;
    @observable isChangePwdModalOpen = false;
    @observable menus = [];
    @observable activeMainMenuKey;
    @observable activeMenuKeys = [];
    @observable expandedKeys = [];
    @observable userInfoData = {};
    @observable enterpriseInfoData = {};

    // 初始用户审核状态为审核中或者审核不通过时展示审核中的企业名称
    @observable tempEnterpriseName;

    @computed
    get menusData() {
        return toJS(this.menus);
    }
    @computed
    get activeMenuKeysData() {
        return this.activeMenuKeys ? toJS(this.activeMenuKeys) : null;
    }

    @computed
    get expandedKeysData() {
        return this.expandedKeys ? toJS(this.expandedKeys) : null;
    }

    @computed
    get userInfo() {
        return this.userInfoData ? toJS(this.userInfoData) : {};
    }

    @computed
    get enterpriseInfo() {
        return this.enterpriseInfoData ? toJS(this.enterpriseInfoData) : {};
    }

    @action
    toggleSideBar = () => {
        this.isSideBarExpanded = !this.isSideBarExpanded;
    };

    @action
    showSideBarToggle = () => {
        this.isSideBarToggleShow = true;
    };

    @action
    expandSideBar = () => {
        this.isSideBarExpanded = true;
    };

    @action
    collapseSideBar = () => {
        this.isSideBarExpanded = false;
    };

    @action
    activeMainMenu = key => {
        this.isSideBarExpanded = true;
        this.isSideBarToggleShow = true;
        this.activeMainMenuKey = key;
    };

    @action
    activeMenuItem = (activeKeys) => {
        this.activeMenuKeys = activeKeys;
        if (activeKeys.length === 2) {
            if (this.expandedKeys && this.expandedKeys.indexOf(activeKeys[1]) !== -1) {
                return;
            }
            if (this.expandedKeys) {
                this.expandedKeys.push(activeKeys[1]);
            } else {
                this.expandedKeys = [activeKeys[1]];
            }
        }
    };

    @action
    openChangePwdModal() {
        this.isChangePwdModalOpen = true;
    }

    @action
    closeChangePwdModal() {
        this.isChangePwdModalOpen = false;
    }

    @action
    async initUserInfo() {
        const res = await this.get('/user/info', null, { showGlobalLoading: true });
        runInAction(() => {
            if (res.code !== '0000') {
                return;
            }
            this.userInfoData = res.data.user || {};
            this.enterpriseInfoData = res.data.enterprise && res.data.enterprise.enterprise_id ? res.data.enterprise : {};
        });
    }

    @action
    async initMenu(path) {
        const res = await this.get('/auth/menus');
        const menuData = res.data || [];
        runInAction(() => {
            this.menus = menuData;
        });
    }

    @action
    setActiveMenus(path) {
        const activeMenuKeys = getActiveKeyFromCurrentPath(this.menusData, path);
        this.activeMenuKeys = activeMenuKeys;
        if (activeMenuKeys.length) {
            this.activeMainMenu(activeMenuKeys[0]);
            if (activeMenuKeys.length === 3) {
                const expandKey = activeMenuKeys[1];
                if (!this.expandedKeys || this.expandedKeys.length < 1) {
                    this.expandedKeys = [expandKey];
                } else if (this.expandedKeys.indexOf(expandKey) === -1) {
                    this.expandedKeys.push(expandKey);
                }
            }
        }
    }

    @action
    logout() {
        this.get('/user/logout');
        this.isSideBarExpanded = false;
        this.isSideBarToggleShow = false;
        this.menus = null;
        this.activeMainMenuKey = null;
        this.activeMenuKeys = null;
        this.expandedKeys = null;
        this.userInfoData = null;
        this.enterpriseInfoData = null;
        this.tempEnterpriseName = null;
    }

    @action
    logger(data) {
        try {
            this.post('/logger', data)   
        } catch(e) {
            console.error('logger error', e)
        }
        
    }
}

function getActiveKeyFromCurrentPath(menus, path) {
    if (!path || path === '/') {
        return [];
    }
    for (let i1 = 0, len1 = menus.length; i1 < len1; i1++) {
        let menu1 = menus[i1];
        let menus1 = menu1.menus;
        if (menus1 && menus1.length) {
            for (let i2 = 0, len2 = menus1.length; i2 < len2; i2++) {
                let menu2 = menus1[i2];
                let menus2 = menu2.menus;
                if (menus2 && menus2.length) {
                    for (let i3 = 0, len3 = menus2.length; i3 < len3; i3++) {
                        let menu3 = menus2[i3];
                        if (menu3.url && path.indexOf(menu3.url) === 0) {
                            return [menu1.id, menu2.id, menu3.id];
                        }
                    }
                } else if (menu2.url && path.indexOf(menu2.url) === 0) {
                    return [menu1.id, menu2.id];
                }
            }
        } else if (menu1.url && path.indexOf(menu1.url) === 0) {
            return [menu1.id];
        }
    }
    return [];
}

export default new LayoutStore();
